package com.example.vue.sys.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.vue.comm.domain.ResultData;
import com.example.vue.comm.util.StringUtil;
import com.example.vue.sys.mapper.SysExceptionMapper;
import com.example.vue.sys.service.SysExceptionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author zhouxl
 * @DATE 2020/12/19
 * @descriptoin
 */
@Component
public class SysExceptionServiceImpl implements SysExceptionService {

    @Autowired
    private SysExceptionMapper sysExceptionMapper;

    @Override
    public Object queryExceptionNum(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> numList = new ArrayList<>();
        dealQueryParams(params);
        List<Map<String, Object>> dateList = dateList(params);
        for (Map<String, Object> date : dateList) {
            params.put("dateTime", date.get("dateTime"));
            Map<String, Object> map = sysExceptionMapper.queryExceptionNum(params);
            numList.add(map);
        }
        result.put("xData", dateList);
        result.put("yData", numList);
        data.setData(result);
        return data;
    }

    @Override
    public Object queryExceptionType(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        dealQueryParams(params);
        List<Map<String, Object>> typeList = sysExceptionMapper.queryExceptionType(params);
        data.setData(typeList);
        return data;
    }

    @Override
    public Object queryExceptionList(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        int pageNum = Integer.parseInt((String) params.get("currentPage"));
        int limit = Integer.parseInt((String) params.get("pageSize"));
        PageHelper.startPage(pageNum, limit);
        List<Map<String, Object>> exceptionList = sysExceptionMapper.queryExceptionList(params);
        data.setData(exceptionList);
        return data;
    }

    @Override
    public Object queryExceptionDetial(Map<String, Object> params) {
        ResultData data = ResultData.success(null);
        Map<String, Object> detailInfo = sysExceptionMapper.queryExceptionDetial(params);
        data.setData(detailInfo);
        return data;

    }

    /**
     * 处理前端的查询参数
     *
     * @return
     */
    public void dealQueryParams(Map<String, Object> params) {
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        String dateType = (String) params.get("dateType");
        if (StringUtil.isEmpty(dateType)) {
            dateType = "1";
        }
        // 解析的日期格式
        String dateFormat = "";
        if ("1".equals(dateType)) {
            dateFormat = "yyyyMMdd";
        } else if ("2".equals(dateType)) {
            dateFormat = "yyyyMM";
        } else if ("3".equals(dateType)) {
            dateFormat = "yyyy";
        }
        if (StringUtil.isEmpty(startTime)) {
            DateTime offset = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, -6);
            startTime = DateUtil.format(offset, "yyyy-MM-dd");
        }
        if (StringUtil.isEmpty(endTime)) {
            endTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        }
        startTime = DateUtil.format(DateUtil.parse(startTime), dateFormat);
        endTime = DateUtil.format(DateUtil.parse(endTime), dateFormat);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("dateFormat", dateFormat);
        params.put("dateType", dateType);
    }

    /**
     * 构建查询的日期参数信息
     *
     * @return
     */
    public List<Map<String, Object>> dateList(Map<String, Object> params) {
        List<Map<String, Object>> dateList = new ArrayList<>();
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        String dateFormat = (String) params.get("dateFormat");
        String dateType = (String) params.get("dateType");
        Date sTime = DateUtil.parse(startTime, dateFormat);
        Date eTime = DateUtil.parse(endTime, dateFormat);
        while (DateUtil.compare(sTime, eTime) <= 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("dateTime", DateUtil.format(sTime, dateFormat));
            if ("1".equals(dateType)) {
                sTime = DateUtil.offset(sTime, DateField.DAY_OF_YEAR, 1);
            } else if ("2".equals(dateType)) {
                sTime = DateUtil.offset(sTime, DateField.MONTH, 1);
            } else if ("3".equals(dateType)) {
                sTime = DateUtil.offset(sTime, DateField.YEAR, 1);
            }
            dateList.add(map);
        }
        return dateList;
    }

}
